package com.qlslylq.ad.example.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.qlslylq.ad.example.R;
import com.qlslylq.ad.example.constant.KeyConst;
import com.qlslylq.ad.sdk.core.listener.SplashAdListener;
import com.qlslylq.ad.sdk.core.loader.SplashAdLoader;
import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.util.Log;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 开屏广告测试页<br/>
 */
public class SplashAdTestActivity extends BaseActivity {

    private SplashAdLoader loader;

    //是否已准备好跳转主页
    private AtomicBoolean canJump = new AtomicBoolean(false);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullScreen();
        setContentView(R.layout.activity_splash_ad_test);
    }

    protected void initView() {
        hideTitleBar();
        boolean fullScreen = getIntent().getBooleanExtra("fullScreen", false);
        if (fullScreen) {
            findView(R.id.layout_logo).setVisibility(View.GONE);
        } else {
            findView(R.id.layout_logo).setVisibility(View.VISIBLE);
        }
    }

    protected void initData() {
        initLoader();
        loader.loadAndShow();
    }

    protected void initListener() {

    }

    protected void onResume() {
        super.onResume();
        routeToHomeActivity();
        canJump.set(true);
    }

    protected void onPause() {
        super.onPause();
        canJump.set(false);
    }

    protected void onRestart() {
        super.onRestart();
        canJump.set(true);
    }

    private void initLoader() {
        if (loader == null) {
            ViewGroup adContainer = findView(R.id.layout_ad_container);
            loader = new SplashAdLoader(this, KeyConst.JH_SPLASH_POS_ID, adContainer, 5 * 1000, new SplashAdListener() {
                public void onAdShow() {
                    Log.i("onAdShow");
                }

                public void onAdShowError(AdError error) {
                    doShowError();
                }

                private void doShowError() {
                    Log.i("onAdShowError");
                    if (isFinishing()) {
                        return;
                    }
                    if (adContainer != null) {
                        adContainer.postDelayed(() -> {
                            showToast("广告加载超时，自动跳转主页~");
                            routeToHomeActivity();
                        }, 2000);
                    } else {
                        routeToHomeActivity();
                    }
                }

                //点击跳过或倒计时时间到了或进入了广告页被迫关闭
                public void onAdDismiss() {
                    Log.i("onAdDismiss");
                    routeToHomeActivity();
                }

                public void onAdClick() {
                    Log.i("onAdClick");
                    showToast("点击了广告~");
                }
            });
        }
    }

    /**
     * 跳转主页<br/>
     */
    private void routeToHomeActivity() {
        if (isFinishing()) {
            return;
        }
        if (canJump.get()) {
            canJump.set(false);
            boolean isLandscape = getIntent().getBooleanExtra("isLandscape", false);
            startActivity(isLandscape, isLandscape ? HomeLActivity.class : HomeActivity.class);
            finish();
        }
    }
}